/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bea.ui;

import bea.banana.BananaKey;
import bea.file.FileProcessor;
import bea.main.Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;


/**
 *
 * @author user1
 */
public class BananaPanel extends JPanel implements ItemListener {
    public JFrame frame;
    public JTextArea in, tex;
    public JPasswordField key;
    public JButton inBrowse, outBrowse, encrypt, decrypt, keySet;
    public JComboBox mode;
    
    
    public JScrollPane texScroll;
    
    public JFileChooser fileIn,fileOut;
    
    public Font fButton = new Font("Verdana", Font.PLAIN ,16);
    public Font fTitle = new Font("Verdana", Font.BOLD ,24);
    public Font fText = new Font("Courier New", Font.PLAIN ,14);
    
    private String[] modeList = {"Choose wisely","ECB (Electronic Code Book)","CBC (Cipher Block Chaining)", "CFB (Cipher Feedback) 8-bit", "OFB 8-bit"};
    
    public String insidetex = "Harusnya diisi";
    
    public int state = 0;
    public static final int STATE_NO = 0;
    public static final int STATE_EN = 1;
    public static final int STATE_DE = 2;
    
    public BananaPanel() {
        super();
        initPanel();
        initFrame();
        initText();
        initButton();
        addComponent();
    }
    
    private void addComponent() {
        
        add(in);
        add(key);
        add(texScroll);
        add(inBrowse);
        add(outBrowse);
        add(keySet);
        add(encrypt);
        add(decrypt);
        add(mode);
        frame.add(this);
        frame.setVisible(true);
    }
    
    private void initPanel() {
        setSize(320,680);
        setLayout(null);
        setBackground(Color.decode("#000000"));
    }
    
    private void initFrame() {
        frame = new JFrame("Banana Encryption");
        frame.setSize(340,680);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }
    
    private void initText() {
        in = new JTextArea("");
        tex = new JTextArea("");
        key = new JPasswordField("");
        
        mode = new JComboBox(modeList);
        mode.addItemListener(this);
        
        in.setBackground(Color.decode("#F2CB05"));
        tex.setBackground(Color.decode("#F2CB05"));
        key.setBackground(Color.decode("#F2CB05"));
        
        in.setFont(fButton);
        tex.setFont(fText);
        key.setFont(fButton);
        
        in.setEditable(false);
        in.setBounds(10,60,200,25);
        key.setEditable(false);
        key.setBounds(10,95,200,25);
        tex.setLineWrap(true);
        tex.setWrapStyleWord(true);
        tex.setEditable(false);
        texScroll = new JScrollPane(tex);
        texScroll.setBounds(10,210,310,360);
        
        mode.setBounds(10,125,310,25);
        mode.setBackground(Color.decode("#F2CB05"));
        mode.setFont(fButton);
       // mode.add();
    }
    
    private void initButton() {
        inBrowse = new JButton("Load");
        outBrowse = new JButton("Save");
        encrypt = new JButton("Encrypt");
        decrypt = new JButton("Decrypt");
        keySet = new JButton("Set Key");
        
        inBrowse.addMouseListener(new Action());
        outBrowse.addMouseListener(new Action());
        encrypt.addMouseListener(new Action());
        decrypt.addMouseListener(new Action());
        keySet.addMouseListener(new Action());
        
        inBrowse.setBounds(220,60,100,25);
        keySet.setBounds(220,95,100,25);
        outBrowse.setBounds(220,585,100,25);
        encrypt.setBounds(10,160,150,25);
        decrypt.setBounds(170,160,150,25);
        
        inBrowse.setBackground(Color.decode("#F2CB05"));
        inBrowse.setFont(fButton);
        outBrowse.setBackground(Color.decode("#F2CB05"));
        outBrowse.setFont(fButton);
        encrypt.setBackground(Color.decode("#F2CB05"));
        encrypt.setFont(fButton);
        decrypt.setBackground(Color.decode("#F2CB05"));
        decrypt.setFont(fButton);
        keySet.setBackground(Color.decode("#F2CB05"));
        keySet.setFont(fButton);
        
        encrypt.setEnabled(false);
        decrypt.setEnabled(false);
        outBrowse.setEnabled(false);
    }
    
    public void setText(String teks) {
        tex.setText(teks.length() > 175280? teks.substring(0, 175280) : teks);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
             encrypt.setEnabled(mode.getSelectedIndex() > 0);
             decrypt.setEnabled(mode.getSelectedIndex() > 0);
        }
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.decode("#F2CB05"));
        g2d.fillRect(0, 0, 350, 50);
        g2d.setColor(Color.decode("#D9A404"));
        g2d.fillRect(0, 50, 350, 150);
        g2d.setColor(Color.decode("#8C6A03"));
        g2d.setFont(fTitle);
        g2d.fillRect(0, 200, 350, 420);
        g2d.drawString(".:BANANA:.", 10, 30);
        g2d.setColor(Color.decode("#F2CB05"));
        g2d.fillRect(0, 620, 350, 40);
        g2d.setColor(Color.decode("#8C6A03"));
        g2d.drawString(".:017:.:052:.:080:.", 10, 645);
    }
}
class Action implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {
        if (((JButton) (e.getSource())).isEnabled()) {
            if (((JButton) (e.getSource())).getText().equals("Load")) {
                FileProcessor.load();
            } else if (((JButton) (e.getSource())).getText().equals("Save")) {
                FileProcessor.save();
            } else if (((JButton) (e.getSource())).getText().equals("Encrypt")) {
                Main.panel.state = BananaPanel.STATE_EN;
                System.out.println("[E]" + new String(BananaKey.kunci));
                //Enkripsi
                FileProcessor.prepare();
                Main.engine.setplain(FileProcessor.input);
                try {
                    switch (Main.panel.mode.getSelectedIndex())
                    {
                        case 0: //choose wisely

                            break;
                        case 1: //EBC
                            Main.engine.ecbEncrypt();
                            break;
                        case 2: //CBC
                            Main.engine.cbcEncrypt();
                            break;
                        case 3: //CFB
                            Main.engine.cfbEncrypt();
                            break;
                        case 4: //OFB
                            Main.engine.ofbEncrypt();
                            break;
                    }
                    byte[] result = Main.engine.gethasil(true);
                    FileProcessor.output = result;
                    Main.panel.setText(new String(result));
                } catch (Exception timoexception) {
                    JOptionPane.showMessageDialog(Main.panel.frame,"Where is your banana bro?");
                }
                //selesai enkripsi
                
                Main.panel.outBrowse.setEnabled(true);
                Main.panel.key.setText("");
            } else if (((JButton) (e.getSource())).getText().equals("Decrypt")) {
                Main.panel.state = BananaPanel.STATE_DE;
                System.out.println("[D]" + new String(BananaKey.kunci));
                //Dekripsi
                Main.engine.setcipher(FileProcessor.input);
                System.out.println(new String(FileProcessor.input));
                try {
                    switch (Main.panel.mode.getSelectedIndex())
                    {
                        case 0: //choose wisely

                            break;
                        case 1: //ECB
                            Main.engine.ecbDecrypt();
                            break;
                        case 2: //CBC
                            Main.engine.cbcDecrypt();
                            break;
                        case 3: //CFB
                            Main.engine.cfbDecrypt();
                            break;
                        case 4: //OFB
                            Main.engine.ofbDecrypt();
                            break;
                    }
                    byte[] result = Main.engine.gethasil(false);
                    System.out.println("JADI : \n"  + new String(result));
                    FileProcessor.confirm(result);
                } catch (Exception timoexception) {
                    JOptionPane.showMessageDialog(Main.panel.frame,"Where is your banana bro?");
                } catch (OutOfMemoryError timoerror) {
                    JOptionPane.showMessageDialog(Main.panel.frame,"Something is wrong with your banana bro!");
                }
                //selesai dekripsi
                
                Main.panel.outBrowse.setEnabled(true);
                Main.panel.key.setText("");
            } else if (((JButton) (e.getSource())).getText().equals("Set Key")) {
                String key = JOptionPane.showInputDialog(Main.panel.frame,"Insert your key : ");
                if (key != null && (!key.isEmpty())) {
                    Main.panel.key.setText(key);
                    Main.engine.key.inputKey(key.getBytes());
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
    
    
}
